package weaver.interfaces.holyview.duss.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-10-14
 * @Description: ${description}
 * @ModifyLog:
 **/

public class DataUtil {
    /*
     *@Description: 其他业务申请单
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2021/10/15
     */
    public static String otherBusinessReceipt(JSONObject paramJson) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        Model.put( "FID", "0" );
        //单据类型-默认为其他业务付款单
        JSONObject FBillTypeID = new JSONObject();
        FBillTypeID.put( "FNUMBER", "FKDLX02_SYS" );
        Model.put( "FBillTypeID", FBillTypeID );
        //业务日期=支付日期
        Model.put( "FDATE", paramJson.get( "rq" ) + " 00:00:00" );
        //往来单位类型-默认为员工
        Model.put( "FCONTACTUNITTYPE", "BD_Empinfo" );
        //往来单位=借款人
        JSONObject FCONTACTUNIT = new JSONObject();
        FCONTACTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FCONTACTUNIT", FCONTACTUNIT );
        //收款单位类型-默认为员工
        Model.put( "FRECTUNITTYPE", "BD_Supplier" );
        //收款单位=借款人
        JSONObject FRECTUNIT = new JSONObject();
        FRECTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FRECTUNIT", FRECTUNIT );
        //币别=默认人民币
        JSONObject FCURRENCYID = new JSONObject();
        FCURRENCYID.put( "FNumber", "PRE001" );
        Model.put( "FCURRENCYID", FCURRENCYID );
        //付款组织=默认为无锡盛景电子股份有限公司
        JSONObject FPAYORGID = new JSONObject();
        FPAYORGID.put( "FNumber", "1001" );
        Model.put( "FPAYORGID", FPAYORGID );
        //结算组织=默认为无锡盛景电子股份有限公司
        JSONObject FSETTLEORGID = new JSONObject();
        FSETTLEORGID.put( "FNumber", "1001" );
        Model.put( "FSETTLEORGID", FSETTLEORGID );

        //付款单明细：FPAYBILLENTRY
        JSONArray FPAYBILLENTRY = new JSONArray();
        JSONObject FPAYBILLENTRYJSON = new JSONObject();
        //结算方式=结算方式
        JSONObject FSETTLETYPEID = new JSONObject();
        FSETTLETYPEID.put( "FNumber", "09" );
        FPAYBILLENTRYJSON.put( "FSETTLETYPEID", FSETTLETYPEID );
        //付款用途=付款用途
        JSONObject FPURPOSEID = new JSONObject();
        FPURPOSEID.put( "FNumber", "SFKYT12_SYS" );
        FPAYBILLENTRYJSON.put( "FPURPOSEID", FPURPOSEID );
        //应付金额=借款金额
        FPAYBILLENTRYJSON.put( "FPAYTOTALAMOUNTFOR", paramJson.get( "je" ) );
        //我方银行账号=支付账户
        JSONObject FACCOUNTID = new JSONObject();
        FACCOUNTID.put( "FNumber", paramJson.get( "zfzh" ) );
        FPAYBILLENTRYJSON.put( "FACCOUNTID", FACCOUNTID );
        FPAYBILLENTRY.add( FPAYBILLENTRYJSON );
        Model.put( "FPAYBILLENTRY", FPAYBILLENTRY );
        params.put( "Model", Model );
        String result = InvokeHelper.Invoke( "Save", "AP_PAYBILL", params.toJSONString() );
        return result;
    }

    /*
     *@Description: 采购业务申请单
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2021/10/15
     */
    public static String procurementBusinessReceipt(JSONObject paramJson) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        String result = "";
        Model.put( "FID", "0" );
        //单据类型-默认为采购业务付款单
        JSONObject FBillTypeID = new JSONObject();
        FBillTypeID.put( "FNUMBER", "FKDLX01_SYS" );
        Model.put( "FBillTypeID", FBillTypeID );
        //业务日期=付款日期
        Model.put( "FDATE", paramJson.get( "rq" ) + " 00:00:00" );
        //往来单位类型-默认供应商
        Model.put( "FCONTACTUNITTYPE", "BD_Supplier" );
        //往来单位-收款单位
        JSONObject FCONTACTUNIT = new JSONObject();
        FCONTACTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FCONTACTUNIT", FCONTACTUNIT );
        //收款单位类型-默认供应商
        Model.put( "FRECTUNITTYPE", "BD_Supplier" );
        //收款单位=收款单位
        JSONObject FRECTUNIT = new JSONObject();
        FRECTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FRECTUNIT", FRECTUNIT );

        Model.put( "FISINIT", false );

        //币别-默认人民币
        JSONObject FCURRENCYID = new JSONObject();
        FCURRENCYID.put( "FNumber", "PRE001" );
        Model.put( "FCURRENCYID", FCURRENCYID );

        Model.put( "FEXCHANGERATE", 1.0 );
        Model.put( "FSETTLERATE", 1.0 );


        //结算组织-默认为无锡盛景电子股份有限公司
        JSONObject FSETTLEORGID = new JSONObject();
        FSETTLEORGID.put( "FNumber", "1001" );
        Model.put( "FSETTLEORGID", FSETTLEORGID );

        //采购组织-默认为无锡盛景电子股份有限公司
        JSONObject FPURCHASEORGID = new JSONObject();
        FPURCHASEORGID.put( "FNumber", "1001" );
        Model.put( "FPURCHASEORGID", FPURCHASEORGID );

        Model.put( "FDOCUMENTSTATUS", "Z" );
        Model.put( "FBUSINESSTYPE", "2" );
        Model.put( "FCancelStatus", "A" );

        //付款组织-默认为无锡盛景电子股份有限公司
        JSONObject FPAYORGID = new JSONObject();
        FPAYORGID.put( "FNumber", "1001" );
        Model.put( "FPAYORGID", FPAYORGID );

        Model.put( "FISSAMEORG", true );
        Model.put( "FIsCredit", false );

        JSONObject FSETTLECUR = new JSONObject();
        FSETTLECUR.put( "FNUMBER", "PRE001" );
        Model.put( "FSETTLECUR", FSETTLECUR );

        Model.put( "FIsWriteOff", false );
        Model.put( "FREALPAY", false );
        Model.put( "FISCARRYRATE", false );

        JSONObject FSETTLEMAINBOOKID = new JSONObject();
        FSETTLEMAINBOOKID.put( "FNUMBER", "PRE001" );
        Model.put( "FSETTLEMAINBOOKID", FSETTLEMAINBOOKID );

        Model.put( "FMoreReceive", false );

        //付款单明细：FPAYBILLENTRY
        JSONArray FPAYBILLENTRY = new JSONArray();
        JSONObject FPAYBILLENTRYJSON = new JSONObject();
        //结算方式-默认为银行转账
        JSONObject FSETTLETYPEID = new JSONObject();
        FSETTLETYPEID.put( "FNumber", "09" );
        FPAYBILLENTRYJSON.put( "FSETTLETYPEID", FSETTLETYPEID );
        //付款用途-默认采购付款
        JSONObject FPURPOSEID = new JSONObject();
        FPURPOSEID.put( "FNumber", "SFKYT08_SYS" );
        FPAYBILLENTRYJSON.put( "FPURPOSEID", FPURPOSEID );
        //应付金额=应付金额
        FPAYBILLENTRYJSON.put( "FPAYTOTALAMOUNTFOR", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FPAYAMOUNTFOR_E", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FSETTLEPAYAMOUNTFOR", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FREALPAYAMOUNTFOR_D", paramJson.get( "je" ) );
        //我方银行账号=支付账户
        JSONObject FACCOUNTID = new JSONObject();
        FACCOUNTID.put( "FNumber", paramJson.get( "zfzh" ) );
        FPAYBILLENTRYJSON.put( "FACCOUNTID", FACCOUNTID );

        FPAYBILLENTRYJSON.put( "FPAYAMOUNT_E", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FPOSTDATE", paramJson.get( "rq" ) + " 00:00:00" );
        FPAYBILLENTRYJSON.put( "FRuZhangType", "1" );
        FPAYBILLENTRYJSON.put( "FPayType", "A" );

        FPAYBILLENTRY.add( FPAYBILLENTRYJSON );
        Model.put( "FPAYBILLENTRY", FPAYBILLENTRY );
        params.put( "Model", Model );
        result = InvokeHelper.Save( "AP_PAYBILL", params.toJSONString() );
        return result;
    }

    /*
     *@Description: 传凭证
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2021/10/15
     */
    public static String passCertificate(JSONObject paramJson) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        String result = "";
        //实体主键
        Model.put( "FVOUCHERID", 0 );
        //账簿
        JSONObject FAccountBookID = new JSONObject();
        FAccountBookID.put( "FNumber", "001" );
        Model.put( "FAccountBookID", FAccountBookID );
        //附件数
        Model.put( "FATTACHMENTS", 2 );
        //日期
        Model.put( "FDate", paramJson.getString( "zfrq" ) + " 00:00:00" );
        //业务日期
        Model.put( "FBUSDATE", paramJson.getString( "zfrq" ) + " 00:00:00" );
        //凭证字
        JSONObject FVOUCHERGROUPID = new JSONObject();
        FVOUCHERGROUPID.put( "FNumber", "PRE001" );
        Model.put( "FVOUCHERGROUPID", FVOUCHERGROUPID );
        //凭证号
        Model.put( "FVOUCHERGROUPNO", "325" );
        //是否调整期凭证
        Model.put( "FISADJUSTVOUCHER", false );
        //审核状态
        Model.put( "FDocumentStatus", "Z" );
        //会计年度
        Model.put( "FYEAR", paramJson.getString( "zfrq" ).substring( 0, 4 ) );
        //业务类型
        JSONObject FSourceBillKey = new JSONObject();
        FSourceBillKey.put( "FNumber", "78050206-2fa6-40e3-b7c8-bd608146fa38" );
        Model.put( "FSourceBillKey", FSourceBillKey );
        //期间
        Model.put( "FPERIOD", 9 );
        //JSONArray FEntity = JSONArray.parseArray(paramJson.getString("FEntity" ));
        Model.put( "FEntity", JSONArray.parseArray( paramJson.getString( "FEntity" ) ) );
        params.put( "Model", Model );
        System.out.println( params );
        result = InvokeHelper.Draft( "GL_VOUCHER", params.toJSONString() );
        return result;
    }

    /*
     *@Description:接口调用日志表
     *@Param:
     *@return:
     *@Author: Dusongsong
     *@Date: 2021/10/14
     */
    public static void saveCallInterLog(String jkmc, String jklx, String fhjg, String requestid, int userid) {
        String curdate = DateUtil.getFullDate();
        int formmodeid = getFormmodeId( "uf_jkdyrzb" );
        Map dataMap = new HashMap();
        dataMap.put( "jkmc", jkmc );
        dataMap.put( "jklx", jklx );
        dataMap.put( "rqsj", curdate );
        dataMap.put( "fhjg", fhjg );
        dataMap.put( "requestid", requestid );
        DataUtil.insertModelData( formmodeid, userid, dataMap );
    }

    //获取formmodeid
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName );
        rs.next();
        int formmodeid = Util.getIntValue( rs.getString( "id" ) );
        return formmodeid;
    }

    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id );
    }

    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate( insSql, params )) {
            rs1.executeQuery( "select id from " + tablename + " where uuid=?", uuid );
            rs1.next();
            dataid = Util.getIntValue( rs1.getString( "id" ) );
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get( key ).toString() + "',";
                }
                if (updatesql.endsWith( "," )) {
                    updatesql = updatesql.substring( 0, updatesql.length() - 1 );
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate( updatesql );
                }
                //进行权限重构
                getNewModeRight( userid, modeid, dataid );
            }
        }
        return dataid;
    }

    public static String getYhzhlb(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select zhmc from uf_yhzhlb where id = ?", id );
        rs.next();
        String zhmc = Util.null2String( rs.getString( "zhmc" ) );
        return zhmc;
    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put( "name", "zhangsan" );
        map.put( "age", "12" );
        String tablename = "formtable_main_13";
        String dataid = "16";

        String updatesql = "insert into " + tablename + "";
        String fields = "";
        String values = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            fields += key + ",";
            values += map.get( key ) + ",";
        }
        fields = fields.substring( 0, fields.length() - 1 );
        values = values.substring( 0, values.length() - 1 );
        updatesql += "(" + fields + ")values (" + values + ")" + " where id=" + dataid;
        System.out.println( updatesql );

    }
}
